package com.maxd.controller;

import com.google.common.collect.Lists;
import com.maxd.adapter.LoginUserHolder;
import com.maxd.bean.PageRes;
import com.maxd.error.ExceptionEnum;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.PromotePointRecord;
import com.maxd.respository.PromotePointRecordRepository;
import com.maxd.utils.AdminUtils;
import com.maxd.utils.AssertUtil;
import com.maxd.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.Predicate;
import java.util.List;

@RestController
@Api(value="推广积分管理", tags = {"推广积分管理"})
@RequestMapping(value = "/promote-point")
public class PromotePointController {

    @Autowired
    private PromotePointRecordRepository promotePointRecordRepository;

    @GetMapping("/promote-point-page")
    @ApiOperation("内管分页查询推广积分记录列表")
    public Result<PageRes<PromotePointRecord>> partnerPage(@ApiParam("页码，从0页开始") @RequestParam("pageNo") Integer pageNo,
                                                           @ApiParam("每页记录数量") @RequestParam("pageSize") Integer pageSize,
                                                           @ApiParam("下单人用户ID") @RequestParam(value = "userId", required = false) Long userId,
                                                           @ApiParam("记录类型 GEN-产生流水 USE-消耗流水") @RequestParam(value = "recordType", required = false) String recordType,
                                                           @ApiParam("场景编码 GEN_BE_PARTNER-成为合伙人获取积分 USE_BUY_PARTNER-购买合伙人使用积分 GEN_BUY_PARTNER-购买合伙人获取积分 ADJUST-手工调整") @RequestParam(value = "sceneCode", required = false) String sceneCode) {
        AdminUtils.checkLogin();
        PageRes<PromotePointRecord> pageRes = queryPage(pageNo, pageSize, userId, recordType, sceneCode);
        return ResultUtil.success(pageRes);
    }

    @GetMapping("/my-promote-point-list")
    @ApiOperation("商城查询我的推广积分记录")
    public Result<PageRes<PromotePointRecord>> partnerList(@ApiParam("页码，从0页开始") @RequestParam("pageNo") Integer pageNo,
                                                           @ApiParam("每页记录数量") @RequestParam("pageSize") Integer pageSize,
                                                           @ApiParam("记录类型 GEN-产生流水 USE-消耗流水") @RequestParam(value = "recordType", required = false) String recordType,
                                                           @ApiParam("场景编码 GEN_BE_PARTNER-成为合伙人获取积分 USE_BUY_PARTNER-购买合伙人使用积分 GEN_BUY_PARTNER-购买合伙人获取积分 ADJUST-手工调整") @RequestParam(value = "sceneCode", required = false) String sceneCode) {
        Long userId = LoginUserHolder.getUserId();
        AssertUtil.assertTrue(userId != null && userId > 0, ExceptionEnum.WRONT_TOKEN);
        PageRes<PromotePointRecord> pageRes = queryPage(pageNo, pageSize, userId, recordType, sceneCode);
        return ResultUtil.success(pageRes);
    }

    private PageRes<PromotePointRecord> queryPage(Integer pageNo, Integer pageSize, Long userId, String recordType, String sceneCode) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.by(order));
        Specification<PromotePointRecord> queryCondition = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = Lists.newArrayList();
            if(userId != null) {
                predicates.add(criteriaBuilder.equal(root.get("userId"), userId));
            }
            if(StringUtils.isNotEmpty(recordType)) {
                predicates.add(criteriaBuilder.equal(root.get("recordType"), recordType));
            }
            if(StringUtils.isNotEmpty(sceneCode)) {
                predicates.add(criteriaBuilder.equal(root.get("sceneCode"), sceneCode));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Page<PromotePointRecord> pageList = promotePointRecordRepository.findAll(queryCondition, pageable);
        return PageUtils.toPageRes(pageList);
    }

}
